package com.bjtu.laorenyouhao.event.service.impl;

import com.bjtu.laorenyouhao.event.feign.LaorenFeignService;
import com.bjtu.laorenyouhao.event.feign.YigongFeignService;
import com.bjtu.laorenyouhao.event.feign.YuangongFeignService;
import com.bjtu.laorenyouhao.event.utils.RenyuanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjtu.common.utils.PageUtils;
import com.bjtu.common.utils.Query;

import com.bjtu.laorenyouhao.event.dao.EventPeopleDao;
import com.bjtu.laorenyouhao.event.entity.EventPeopleEntity;
import com.bjtu.laorenyouhao.event.service.EventPeopleService;
import org.springframework.util.StringUtils;


@Service("eventPeopleService")
public class EventPeopleServiceImpl extends ServiceImpl<EventPeopleDao, EventPeopleEntity> implements EventPeopleService {

    @Autowired
    private LaorenFeignService laorenFeignService;

    @Autowired
    private YigongFeignService yigongFeignService;

    @Autowired
    private YuangongFeignService yuangongFeignService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        QueryWrapper<EventPeopleEntity> queryWrapper = new QueryWrapper<EventPeopleEntity>();
        if (StringUtils.hasLength(key)) {

            Integer laorenId = RenyuanUtils.getLaorenId(key, laorenFeignService);
            Integer yigongId = RenyuanUtils.getYigongId(key, yigongFeignService);
            Integer yuangongId = RenyuanUtils.getYuangongId(key, yuangongFeignService);

            Integer renyuanId = 0;

            if (laorenId != null){
                renyuanId = laorenId;
            }
            if (yigongId != null){
                renyuanId = yigongId;
            }
            if (yuangongId != null){
                renyuanId = yuangongId;
            }


            System.out.println(key);
            queryWrapper.eq("event_id", key)
                    .or().eq("renyuan_id", renyuanId);

        }

        IPage<EventPeopleEntity> page = this.page(
                new Query<EventPeopleEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}